package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkTask;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues.Article;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 获取作业详情
 */
@CrossOrigin
@RestController
public class WorkInfoGetController extends YxyApiBaseController {

    @Autowired
    ElasticQuesService elasticQuesService;

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    HomeworkTaskRepository HomeworkTaskRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/getwork", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;

        // 任务详情
        HomeworkTask homeworkTask = HomeworkTaskRepository.findOne(taskId);
        /** 单选类试题 */
        List<HomeworkTaskQues> homeworkTaskQuesList0 = homeworkTaskQuesRepository.findByTaskIdAndQuesType(taskId, "0");
        /** 多选类试题 */
        List<HomeworkTaskQues> homeworkTaskQuesList1 = homeworkTaskQuesRepository.findByTaskIdAndQuesType(taskId, "1");
        /** 通知类题 */
        List<HomeworkTaskQues> homeworkTaskQuesList2 = homeworkTaskQuesRepository.findByTaskIdAndQuesType(taskId, "2");
        /** 填空题 */
        List<HomeworkTaskQues> homeworkTaskQuesList3 = homeworkTaskQuesRepository.findByTaskIdAndQuesType(taskId, "3");
        /** 简答题 */
        List<HomeworkTaskQues> homeworkTaskQuesList4 = homeworkTaskQuesRepository.findByTaskIdAndQuesType(taskId, "4");

        // 获取试题Id
        List<String> quesIds = new ArrayList<>();
        homeworkTaskQuesList0.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        homeworkTaskQuesList1.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        homeworkTaskQuesList3.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        homeworkTaskQuesList4.forEach(homeworkTaskQues -> {
            quesIds.add(homeworkTaskQues.getQuesId());
        });
        List<ElasticYxyYxyQuesResource> questions = elasticQuesService.findByQuesIdIn(quesIds);
        Map<String, ElasticYxyYxyQuesResource> questionMap = new HashMap<String, ElasticYxyYxyQuesResource>();
        questions.forEach(question -> {
            questionMap.put(question.getId(), question);
        });

        // 单选题
        SingleQues singleQues = new SingleQues();
        singleQues.total = homeworkTaskQuesList0.size();
        List<Question> singleQuestions = new ArrayList<Question>();
        homeworkTaskQuesList0.forEach(homeworkTaskQues -> {
            Question question = new Question();
            question.index = homeworkTaskQues.getOrder();
            question.ques = null;
            try {
                question.ques = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            singleQuestions.add(question);
        });
        singleQues.questions = singleQuestions;
        // 多选题
        MultiQues multiQues = new MultiQues();
        multiQues.total = homeworkTaskQuesList1.size();
        List<Question> multiQuestions = new ArrayList<Question>();
        homeworkTaskQuesList1.forEach(homeworkTaskQues -> {
            Question question = new Question();
            question.index = homeworkTaskQues.getOrder();
            question.ques = null;
            try {
                question.ques = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            multiQuestions.add(question);
        });
        multiQues.questions = multiQuestions;
        // 通知类试题
        NotifyQues notifyQues = new NotifyQues();
        notifyQues.questionText = homeworkTaskQuesList2.size() == 0 ? null
                : homeworkTaskQuesList2.get(0).getQuesContent();
        notifyQues.quesImage = homeworkTaskQuesList2.size() == 0 ? null : homeworkTaskQuesList2.get(0).getQuesImage();
        notifyQues.quesArticle = homeworkTaskQuesList2.size() == 0 ? null
                : homeworkTaskQuesList2.get(0).getQuesArticle();
        notifyQues.ifNeedSumbit = homeworkTask == null ? true
                : homeworkTask.getIfNeedSumbit() == null ? true : homeworkTask.getIfNeedSumbit();
        // 填空题
        BlankFillsQues blankFillsQues = new BlankFillsQues();
        blankFillsQues.total = homeworkTaskQuesList3.size();
        List<Question> blankFillsQuestions = new ArrayList<Question>();
        homeworkTaskQuesList3.forEach(homeworkTaskQues -> {
            Question question = new Question();
            question.index = homeworkTaskQues.getOrder();
            question.ques = null;
            try {
                question.ques = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            blankFillsQuestions.add(question);
        });
        blankFillsQues.questions = blankFillsQuestions;
        // 简答题
        ShortQues shortQues = new ShortQues();
        shortQues.total = homeworkTaskQuesList4.size();
        List<Question> shortQuestions = new ArrayList<Question>();
        homeworkTaskQuesList4.forEach(homeworkTaskQues -> {
            Question question = new Question();
            question.index = homeworkTaskQues.getOrder();
            question.ques = null;
            try {
                question.ques = questionMap.get(homeworkTaskQues.getQuesId());
            } catch (Exception e) {
            }
            shortQuestions.add(question);
        });
        shortQues.questions = shortQuestions;
        // return
        respModel.title = homeworkTask.getTitle() == null ? "暂无标题" : homeworkTask.getTitle();
        respModel.publishTime = "发布时间："
                + DateToolUtils.format(homeworkTask.getPublishDate(), DateToolUtils.FORMAT_LONG_CN);
        respModel.singleQues = singleQues;
        respModel.multiQues = multiQues;
        respModel.notifyQues = notifyQues;
        respModel.blankFillsQues = blankFillsQues;
        respModel.shortQues = shortQues;
        return respModel;
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 用户Id (不用传参数)
        public String userId;
        // 学校Id (不用传参数)
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // title
        public String title;
        // publishTime
        public String publishTime;
        // 单选题
        public SingleQues singleQues;
        // 多选题
        public MultiQues multiQues;
        // 通知类试题
        public NotifyQues notifyQues;
        // 填空题
        public BlankFillsQues blankFillsQues;
        // 简单题
        public ShortQues shortQues;
    }

    /** 单选题 */
    public static class SingleQues {
        // 试题总数量
        public Integer total;
        // 试题列表
        public List<Question> questions;
    }

    /** 多选题 */
    public static class MultiQues {
        // 试题总数量
        public Integer total;
        // 试题列表
        public List<Question> questions;
    }

    /** 填空题 */
    public static class BlankFillsQues {
        // 试题总数量
        public Integer total;
        // 试题列表
        public List<Question> questions;
    }

    /** 简答题 */
    public static class ShortQues {
        // 试题总数量
        public Integer total;
        // 试题列表
        public List<Question> questions;
    }

    /** 通知类 */
    public static class NotifyQues {
        // 通知类作业，上传的图片Url
        public List<String> quesImage;
        // 通知类作业，上传的文档,需要存储文档地址，文档的名称
        public List<Article> quesArticle;
        // 通知类作业的题干
        public String questionText;
        // 通知类作业是否需要学生在线提交true需要,false不需要
        public Boolean ifNeedSumbit = true;
    }

    /** 试题详情 */
    public static class Question {
        // 试题在任务中的序号
        public Integer index;
        // 试题 Object
        public Object ques;
        // show
        public boolean show = false;
    }

}
